package com.yidu.web.dao;

import com.yidu.web.model.Crosstalklist;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * 信息dao
 */
public class CrosstalkDao {

    /**
     * 增加
     * @param crosstalklist
     * @return
     */
    public int insert(Crosstalklist crosstalklist) {
        Connection con = null;
        PreparedStatement statement = null;

        try {
            con = JDBCTools.getCon();
            String sql = "insert into crosstalklist(CrossTalkID,CrossTalkTitle,CrossTalkA,CrossTalkContent,flag) values (null, ?, ?, ?, ?)";

            statement = con.prepareStatement(sql);
            statement.setString(1, crosstalklist.getCrossTalkTitle());
            statement.setString(2, crosstalklist.getCrossTalkA());
            statement.setString(3, crosstalklist.getCrossTalkContent());
            statement.setString(4, crosstalklist.getFlag());
            System.out.println(sql);

            return statement.executeUpdate();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.close(con, statement);
        }
        return 0;
    }

    public Map<String,Crosstalklist> selectFlag(){
        Map<String,Crosstalklist> map= new HashMap<>();
        Connection con = null;
        PreparedStatement statement = null;
        ResultSet rs = null;

        try {
            con = JDBCTools.getCon();
            String sql = "select * from crosstalklist";
            statement = con.prepareStatement(sql);
            rs = statement.executeQuery();
            while(rs.next()){
                Crosstalklist crosstalklist = new Crosstalklist();
                String key = rs.getString("flag");
                crosstalklist.setFlag(key);
                Integer id = rs.getInt("CrossTalkID");
                crosstalklist.setCrossTalkId(id);
                String title = rs.getString("CrossTalkTitle");
                crosstalklist.setCrossTalkTitle(title);
                String content = rs.getString("CrossTalkContent");
                crosstalklist.setCrossTalkContent(content);
                map.put(key,crosstalklist);
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.close(con,statement,rs);
        }

        return null;
    }
}
